package com.machine.filling.net.serialPort.util;

import com.machine.filling.net.serialPort.send.CMD_Base;
import com.machine.filling.util.LogUtil;
import com.machine.filling.util.NumberUtil;
import com.machine.filling.util.StringUtil;

/**
 * Created by Administrator on 2018/3/11 0011.
 */

public class SerialPortSendUtil {
    private static SerialPortSendUtil instance;

    public static SerialPortSendUtil getInstance() {
        if (instance == null) {
            synchronized (SerialPortSendUtil.class) {
                if (instance == null)
                    instance = new SerialPortSendUtil();
            }
        }
        return instance;
    }

    private SerialPortSendUtil() {

    }

    /**
     * @param versionHex 命令中的版本号
     * @param gunNumHex  油枪枪号
     * @return
     */
    public String getCmd_LockGun_1102(String versionHex, String gunNumHex) {
        CMD_Base cmd_send_lock = new CMD_Base();
        cmd_send_lock.setVersion_1(versionHex);
        cmd_send_lock.setGunNum_1(gunNumHex);
        cmd_send_lock.setCmd_2("1102");
        cmd_send_lock.setDatas("");
        return cmd_send_lock.getCMDFrame();
    }

    /**
     * 解锁油枪
     *
     * @param versionHex
     * @param gunNumHex
     * @return
     */
    public String getCmd_UnlockGun_1103(String versionHex, String gunNumHex) {
        CMD_Base cmd_unLock = new CMD_Base();
        cmd_unLock.setVersion_1(versionHex);
        cmd_unLock.setGunNum_1(gunNumHex);
        cmd_unLock.setCmd_2("1103");
        cmd_unLock.setDatas("");
        return cmd_unLock.getCMDFrame();
    }

    /**
     * 控制油机 自助非自助
     *
     * @param versionHex
     * @param gunNumHex
     * @param isSelf     是否自助
     * @return
     */
    public String getCmd_IsSelf_910E(String versionHex, String gunNumHex, boolean isSelf) {
        CMD_Base isSelfCmd = new CMD_Base();
        isSelfCmd.setVersion_1(versionHex);
        gunNumHex = "4";
        if (gunNumHex.length() == 1) {
            gunNumHex = "0" + gunNumHex;
        }
        byte[] guns = NumberUtil.IntToByte(Integer.valueOf(gunNumHex));
        byte[] by = new byte[1];
        by[0] = guns[3];
        isSelfCmd.setGunNum_1(StringUtil.bytesToHexString(by));
        isSelfCmd.setCmd_2("910E");
        //TODO  20190719 更改添加四个字节
        //        isSelfCmd.setDatas(isSelf ? "3333" : "3331");
        //        isSelfCmd.setDatas(isSelf ? "3333" : "3332");
        isSelfCmd.setDatas("333330303030");
        //        isSelfCmd.setDatas(isSelf ? "333300000000" : "333100000000");
        //        isSelfCmd.setDatas("3333");
        return isSelfCmd.getCMDFrame();
    }


    /**
     * 下发卡信息
     *
     * @param versionHex
     * @param gunNumHex
     * @param datas      数据域
     * @return
     */
    public String getCmd_SendCardInfo_1101(String versionHex, String gunNumHex, String datas) {
        CMD_Base cmd_unLock = new CMD_Base();
        cmd_unLock.setVersion_1(versionHex);
        cmd_unLock.setGunNum_1(gunNumHex);
        cmd_unLock.setCmd_2("1101");
        cmd_unLock.setDatas(datas);
        return cmd_unLock.getCMDFrame();
    }

    /**
     * 下发写卡信息
     *
     * @param versionHex
     * @param gunNumHex
     * @param datas
     * @return
     */
    public String getCmd_WriteCard_9101(String versionHex, String gunNumHex, String datas) {
        CMD_Base writeCard = new CMD_Base();
        writeCard.setVersion_1(versionHex);
        writeCard.setGunNum_1(gunNumHex);
        writeCard.setCmd_2("9101");
        writeCard.setDatas(datas);
        return writeCard.getCMDFrame();
    }

    /**
     * 确认交易
     *
     * @param versionHex
     * @param gunNumHex
     * @param pingzhengCode
     * @return
     */
    public String getCmd_DealConfirm_1002(String versionHex, String gunNumHex, String pingzhengCode) {
        CMD_Base baseCard = new CMD_Base();
        baseCard.setVersion_1(versionHex);
        baseCard.setGunNum_1(gunNumHex);
        baseCard.setCmd_2("1002");
        baseCard.setDatas(gunNumHex + pingzhengCode);
        return baseCard.getCMDFrame();
    }

    /**
     * 设置油机
     *
     * @param versionHex
     * @param gunNumHex
     * @param datas
     * @return
     */
    public String getCmd_SetMachine_1104(String versionHex, String gunNumHex, String datas) {
        CMD_Base baseCard = new CMD_Base();
        baseCard.setVersion_1(versionHex);
        baseCard.setGunNum_1(gunNumHex);
        baseCard.setCmd_2("1104");
        baseCard.setDatas(datas);
        return baseCard.getCMDFrame();
    }

    /**
     * 修改卡机
     *
     * @param versionHex
     * @param gunNumHex
     * @param datas
     * @return
     */
    public String getCmd_SetKj_910A(String versionHex, String gunNumHex, String datas) {
        CMD_Base baseCard = new CMD_Base();
        baseCard.setVersion_1(versionHex);
        baseCard.setGunNum_1(gunNumHex);
        baseCard.setCmd_2("910A");
        baseCard.setDatas(datas);
        return baseCard.getCMDFrame();
    }

    /**
     * 预约加油  定量
     *
     * @param versionHex
     * @param gunNumHex
     * @param datas
     * @return
     */
    public String getCmd_StartOrderFueling110A(String versionHex, String gunNumHex, String datas) {
        CMD_Base baseCard = new CMD_Base();
        baseCard.setVersion_1(versionHex);
        baseCard.setGunNum_1(gunNumHex);
        baseCard.setCmd_2("110A");
        baseCard.setDatas(datas);
        return baseCard.getCMDFrame();
    }

    /**
     * 取消预约加油
     *
     * @param versionHex
     * @param gunNumHex
     * @param datas
     * @return
     */
    public String getCmd_StartOrderFueling110B(String versionHex, String gunNumHex, String datas) {
        CMD_Base baseCard = new CMD_Base();
        baseCard.setVersion_1(versionHex);
        baseCard.setGunNum_1(gunNumHex);
        baseCard.setCmd_2("110B");
        baseCard.setDatas(datas);
        return baseCard.getCMDFrame();
    }

    /**
     * 查询总累计
     *
     * @param versionHex
     * @param gunNumHex
     * @return
     */
    public String getCmd_QueryLeiji_1105(String versionHex, String gunNumHex) {
        CMD_Base baseCard = new CMD_Base();
        baseCard.setVersion_1(versionHex);
        baseCard.setGunNum_1(gunNumHex);
        baseCard.setCmd_2("1105");
        baseCard.setDatas("00");
        return baseCard.getCMDFrame();
    }
}
